<#-- 美化版后台管理页面 FTL 模板 -->
<template>
  <div class="${domain}-container">
    <div class="header">
      <h1 class="title">${tableNameCn}管理</h1>
      <p class="subtitle">${tableComment!tableNameCn}信息管理，包括<#list fieldList as field><#if field?index != 0>、</#if>${field.nameCn}</#list></p>
    </div>

    <a-card class="operation-card">
      <div class="operation-bar">
        <a-space>
          <a-button type="primary" @click="handleQuery()" class="refresh-btn">
            <template #icon><reload-outlined /></template>
            刷新数据
          </a-button>
          <#if !readOnly>
          <a-button type="primary" @click="onAdd" class="add-btn">
            <template #icon><plus-outlined /></template>
            新增${tableNameCn}
          </a-button>
          </#if>
        </a-space>
        <div class="statistics">
          <div class="stat-item">
            <dashboard-outlined />
            <span>${tableNameCn}总数: {{ pagination.total }}</span>
          </div>
        </div>
      </div>
    </a-card>

    <a-card class="table-card">
      <a-table
        :dataSource="${domain}s"
        :columns="columns"
        :pagination="pagination"
        @change="handleTableChange"
        :loading="loading"
        :locale="{ emptyText: '暂无${tableNameCn}数据' }"
        rowKey="id"
        bordered
      >
        <template #bodyCell="{ column, record }">
          <#list fieldList as field>
            <#if field.enums>
<#--                下面需要手动修改-->
          <template v-else-if="column.dataIndex === '${field.nameHump}'">
            <a-tag :color="record.${field.nameHump} === 'Y' ? 'green' : 'orange'">
              <span v-for="item in ${field.enumsConst}_ARRAY" :key="item.code">
                <span v-if="item.code === record.${field.nameHump}">{{item.desc}}</span>
              </span>
            </a-tag>
          </template>
            </#if>
          </#list>
          <#if !readOnly>
          <template v-else-if="column.dataIndex === 'operation'">
            <a-space class="operation-buttons">
              <a-tooltip title="编辑${tableNameCn}信息">
                <a-button type="primary" size="small" @click="onEdit(record)" class="edit-btn">
                  <template #icon><edit-outlined /></template>
                </a-button>
              </a-tooltip>
              <a-popconfirm
                title="确定要删除此${tableNameCn}吗？"
                ok-text="确认"
                cancel-text="取消"
                @confirm="onDelete(record)"
              >
                <a-tooltip title="删除${tableNameCn}">
                  <a-button type="primary" danger size="small" class="delete-btn">
                    <template #icon><delete-outlined /></template>
                  </a-button>
                </a-tooltip>
              </a-popconfirm>
            </a-space>
          </template>
          </#if>
        </template>
      </a-table>
    </a-card>

    <#if !readOnly>
    <a-modal
      v-model:visible="visible"
      :title="${domain}.id ? '编辑${tableNameCn}' : '添加新${tableNameCn}'"
      @ok="handleOk"
      ok-text="确认"
      cancel-text="取消"
      :width="600"
      centered
      class="${domain}-modal"
    >
      <a-form :model="${domain}" :label-col="{span: 6}" :wrapper-col="{ span: 16 }">
        <#list fieldList as field>
          <#if field.name!="id" && field.nameHump!="createTime" && field.nameHump!="updateTime">
        <a-form-item label="${field.nameCn}"<#if field.required!false> required</#if>>
            <#if field.enums>
          <a-select v-model:value="${domain}.${field.nameHump}" placeholder="请选择${field.nameCn}">
            <a-select-option v-for="item in ${field.enumsConst}_ARRAY" :key="item.code" :value="item.code">
              {{item.desc}}
            </a-select-option>
          </a-select>
            <#elseif field.javaType=='Date'>
              <#if field.type=='time'>
          <a-time-picker v-model:value="${domain}.${field.nameHump}" valueFormat="HH:mm:ss" placeholder="请选择时间" style="width: 100%" />
              <#elseif field.type=='date'>
          <a-date-picker v-model:value="${domain}.${field.nameHump}" valueFormat="YYYY-MM-DD" placeholder="请选择日期" style="width: 100%" />
              <#else>
          <a-date-picker v-model:value="${domain}.${field.nameHump}" valueFormat="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择日期时间" style="width: 100%" />
              </#if>
            <#else>
          <a-input v-model:value="${domain}.${field.nameHump}" placeholder="请输入${field.nameCn}" allow-clear />
            </#if>
        </a-form-item>
          </#if>
        </#list>
      </a-form>
      <template #footer>
        <a-button key="back" @click="visible = false">取消</a-button>
        <a-button key="submit" type="primary" @click="handleOk">提交</a-button>
      </template>
    </a-modal>
    </#if>
  </div>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue';
import { notification } from "ant-design-vue";
import axios from "axios";
import {
  ReloadOutlined, PlusOutlined, EditOutlined,
  DeleteOutlined, DashboardOutlined
} from '@ant-design/icons-vue';

export default defineComponent({
  name: "${domain}-view",
  components: {
    ReloadOutlined,
    <#if !readOnly>PlusOutlined, EditOutlined, DeleteOutlined,</#if>
    DashboardOutlined
  },
  setup() {
    <#list fieldList as field>
    <#if field.enums>
    const ${field.enumsConst}_ARRAY = window.${field.enumsConst}_ARRAY;
    </#if>
    </#list>
    const visible = ref(false);
    let ${domain} = ref({
      <#list fieldList as field>
      ${field.nameHump}: undefined<#if field_has_next>,</#if>
      </#list>
    });
    const ${domain}s = ref([]);
    const pagination = ref({
      total: 0,
      current: 1,
      pageSize: 10,
    });
    let loading = ref(false);
    const columns = [
      <#list fieldList as field>
        <#if field.name!="id" && field.nameHump!="createTime" && field.nameHump!="updateTime">
      {
        title: '${field.nameCn}',
        dataIndex: '${field.nameHump}',
        key: '${field.nameHump}',
      },
        </#if>
      </#list>
      <#if !readOnly>
      {
        title: '操作',
        dataIndex: 'operation',
        align: 'center',
        width: '150px'
      }
      </#if>
    ];

    <#if !readOnly>
    const onAdd = () => {
      ${domain}.value = {};
      visible.value = true;
    };

    const onEdit = (record) => {
      ${domain}.value = window.Tool.copy(record);
      visible.value = true;
    };

    const onDelete = (record) => {
      axios.delete("/${directory}/admin/${do_main}/delete/" + record.id).then((response) => {
        const data = response.data;
        if (data.success) {
          notification.success({
            message: '删除成功',
            <#--description: `${tableNameCn} ${record.name || record.id} 已删除`,-->
          });
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize,
          });
        } else {
          notification.error({description: data.message});
        }
      });
    };

    const handleOk = () => {
      axios.post("/${directory}/admin/${do_main}/save", ${domain}.value).then((response) => {
        let data = response.data;
        if (data.success) {
          notification.success({
            message: '保存成功',
            description: ${domain}.value.id ? '${tableNameCn}信息已更新' : '新${tableNameCn}已添加',
          });
          visible.value = false;
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });
        } else {
          notification.error({description: data.message});
        }
      });
    };
    </#if>

    const handleQuery = (param) => {
      if (!param) {
        param = {
          page: 1,
          size: pagination.value.pageSize
        };
      }
      loading.value = true;
      axios.get("/${directory}/admin/${do_main}/query-list", {
        params: {
          page: param.page,
          size: param.size
        }
      }).then((response) => {
        loading.value = false;
        let data = response.data;
        if (data.success) {
          ${domain}s.value = data.content.list;
          pagination.value.current = param.page;
          pagination.value.total = data.content.total;
        } else {
          notification.error({description: data.message});
        }
      }).catch(() => {
        loading.value = false;
      });
    };

    const handleTableChange = (pagination) => {
      handleQuery({
        page: pagination.current,
        size: pagination.pageSize
      });
    };

    onMounted(() => {
      handleQuery({
        page: 1,
        size: pagination.value.pageSize
      });
    });

    return {
      <#list fieldList as field>
        <#if field.enums>
      ${field.enumsConst}_ARRAY,
        </#if>
      </#list>
      ${domain},
      visible,
      ${domain}s,
      pagination,
      columns,
      handleTableChange,
      handleQuery,
      loading,
      <#if !readOnly>
      onAdd,
      handleOk,
      onEdit,
      onDelete
      </#if>
    };
  },
});
</script>

<style scoped>
.${domain}-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
}

.header {
  margin-bottom: 24px;
  text-align: center;
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #1f2f3d;
  margin-bottom: 8px;
  background: linear-gradient(90deg, #1890ff, #722ed1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  color: #606266;
  font-size: 16px;
}

.operation-card {
  margin-bottom: 24px;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.table-card {
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.operation-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.refresh-btn, .add-btn {
  border-radius: 6px;
  font-weight: 500;
  padding: 0 20px;
  height: 36px;
}

.add-btn {
  background: linear-gradient(90deg, #13c2c2, #1890ff);
  border: none;
}

.statistics {
  display: flex;
  gap: 20px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f0f9ff;
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 500;
  color: #1890ff;
}

.operation-buttons {
  display: flex;
  justify-content: center;
}

.edit-btn {
  background: linear-gradient(90deg, #52c41a, #389e0d);
  border: none;
}

.delete-btn {
  border: none;
}

:deep(.ant-table-thead > tr > th) {
  background-color: #f0f9ff !important;
  font-weight: 600;
  color: #1f2f3d;
}

:deep(.ant-table-row) {
  transition: background-color 0.3s;
}

:deep(.ant-table-row:hover) {
  background-color: #f0f9ff !important;
}

.${domain}-modal :deep(.ant-modal-header) {
  border-radius: 10px 10px 0 0;
}

.${domain}-modal :deep(.ant-modal-body) {
  padding: 24px;
}

.${domain}-modal :deep(.ant-form-item) {
  margin-bottom: 20px;
}

.${domain}-modal :deep(.ant-input) {
  border-radius: 6px;
  height: 40px;
}

@media (max-width: 768px) {
  .operation-bar {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }

  .${domain}-container {
    padding: 15px;
  }

  .title {
    font-size: 24px;
  }

  .statistics {
    width: 100%;
    justify-content: flex-start;
  }
}
</style>